#######################################################
# Generalized Synthetic Control Method: Crime Concerns
#######################################################

rm(list=ls())

library(gsynth)
library(panelView)
set.seed(1111)

#sink("16_gsynth.txt")

# Load data
load("final_county_2023april6.RData")
names(d_county)

###############################################
# Concerns Haiti
###############################################

system.time(out1 <- gsynth(crime_concerns_s ~ t_stagg_haiti, 
                           data = d_county, 
                           index = c("county","survey"), 
                           force = "two-way",
                           CV = TRUE, 
                           se = TRUE, 
                           r = c(0,4),
                           nboots = 10, 
                           min.T0 = 6, # six for consistency across all estimations
                           seed = 10))
out1$est.avg

###############################################
# Concerns All
###############################################

system.time(out2 <- gsynth(crime_concerns_s ~ t_stagg_all, 
                           data = d_county, 
                           index = c("county","survey"), 
                           force = "two-way",
                           CV = TRUE, 
                           se = TRUE, 
                           r = c(0,4),
                           nboots = 10, 
                           min.T0 = 6,
                           seed = 10))
out2$est.avg

###############################################
# Rates Haiti
###############################################

system.time(out3 <- gsynth(crime_rates_s ~ t_stagg_haiti, 
                           data = d_county, 
                           index = c("county","survey"), 
                           force = "two-way",
                           CV = TRUE, 
                           se = TRUE, 
                           r = c(0,4),
                           nboots = 10, 
                           min.T0 = 6,
                           seed = 10))
out3$est.avg

###############################################
# Rates All
###############################################

system.time(out4 <- gsynth(crime_rates_s ~ t_stagg_all, 
                           data = d_county, 
                           index = c("county","survey"), 
                           force = "two-way",
                           CV = TRUE, 
                           se = TRUE, 
                           r = c(0,4),
                           nboots = 10, 
                           min.T0 = 6,
                           seed = 10))
out4$est.avg

###############################################
# Table
###############################################

pe1 = round(out1$est.avg[1],3)
se1 = round(out1$est.avg[2],3)
pe2 = round(out2$est.avg[1],3)
se2 = round(out2$est.avg[2],3)
pe3 = round(out3$est.avg[1],3)
se3 = round(out3$est.avg[2],3)
pe4 = round(out4$est.avg[1],3)
se4 = round(out4$est.avg[2],3)

columns = c("visas from Haiti","","visas from all countries","","covariates")
concerns = c(pe1,se1,pe2,se2,"no")
rates = c(pe3,se3,pe4,se4,"no")
results = cbind(columns,concerns,rates)
results

# Table a9
write.table(results, "tableA9.txt", sep="\t")

sink()
